#!/bin/bash
#$Id: promon.wrap,v 1.1 2012-05-07 13:48:52 remik Exp $
#
# This script should perform no changes, only notice non standard configurations
#
# Load usefull functions
if [ ! -f $HOME/scripto/bash/bash_library.sh ]; then
  echo "[error] $HOME/scripto/bash/bash_library.sh not found. Exiting. "
  exit 1
else
  . $HOME/scripto/bash/bash_library.sh
fi

F_OUTPUT=/tmp/promon.output.tmp
#INFO_MODE=ERROR
INFO_MODE=DEBUG
export INFO_MODE

#### BEGIN FUNCTIONS SECTION
# Take file privided as $1
# Copy to directory already in CVS provided as $2
# store it in CVS as filename provided as $3
f_standalone_file_to_cvs()
{
  msgi "[f_standalone_file_to_cvs] Storing the: $1 in CVS"
  msgi "[f_standalone_file_to_cvs] In dir: $2 as filename: $3"
  check_file "$1"
  
  # if the directory does not exists, try to create it and add to CVS
  if [ -d $2 ]; then
    msgd "Dir: $2 exists. OK."
  else
    msgd "Dir: $2 NOT exists. Creating one and adding to CVS."
    run_command_e "mkdir -p $2"
    run_command_e "cd $2"
    run_command_e "cd .."
    V_CVS_DIR=`basename $2`
    msgd "V_CVS_DIR: $V_CVS_DIR"
    cvs add $V_CVS_DIR > /dev/null 2>&1
  fi

  check_parameter "$3"

  cd $2
  cp $1 $3
  cvs add $3 > /dev/null 2>&1
  V_CVSDIFF=`cvs diff $3 | $GREP -e "^>" -e "^<"`
  if [ `echo $V_CVSDIFF | grep -v '^ *$' | wc -l ` -gt 0 ]; then
    #echo $V_CVSDIFF
    cvs diff $3
  fi
  cvs commit -m "[auto_commit_to_cvs] $V_CVSDIFF" $3 > /dev/null 2>&1

} #f_standalone_file_to_cvs


f_sql_results_to_cvs()
{
  msgd "Store sql results in CVS"
  F_SQL_FILENAME=$1
  D_CVS_DIR=$2
  F_CVS_FILENAME=$3

  check_parameter $F_SQL_FILENAME
  check_parameter $D_CVS_DIR
  check_parameter $F_CVS_FILENAME

  D_SQL_DIR=$HOME/scripto/oracle/monitoring/promon/sql
  check_directory $D_SQL_DIR
  check_file $D_SQL_DIR/$F_SQL_FILENAME

  V_TMP=`cat $D_SQL_DIR/$F_SQL_FILENAME`
  msgd "V_TMP: $V_TMP"
  f_execute_sql "$V_TMP"

  msgd "F_EXECUTE_SQL: $F_EXECUTE_SQL"
  run_command_d "cat $F_EXECUTE_SQL"

exit 0

} #f_sql_results_to_cvs

#### END FUNCTIONS SECTION

msgd "Check if configuration file is already present"
F_CONF=$HOME/local_scripto/promon/promon_${USERNAME}_${HOSTNAME}.conf
msgd "F_CONF: $F_CONF"

if [ -f "$F_CONF" ]; then
  msgd "Conf file found."
else
  msgd "No Conf file found."
  msge "Exiting. Not yet implemented."
  exit 1
fi

msgd "Read the conf file"
while read F_CONF_LINE
do
  msgd "F_CONF_LINE: $F_CONF_LINE"
  V_TYPE=`echo $F_CONF_LINE | awk -F"#" '{print $1}'`
  msgd "V_TYPE: $V_TYPE"

  V_ARGUMENT=`echo $F_CONF_LINE | awk -F"#" '{print $2}'`
  msgd "V_ARGUMENT: $V_ARGUMENT"

  case $V_TYPE in
  "file")
    msgd "Type: file"
    # I will construct the name of the file in CVS based on the filename provided
    V_CVS_FILENAME=`basename $V_ARGUMENT | sed 's/\.//'`
    V_CVS_FILENAME=${V_CVS_FILENAME}
    msgd "V_CVS_FILENAME: $V_CVS_FILENAME"
    f_standalone_file_to_cvs $V_ARGUMENT $HOME/local_scripto/oracle/logs/${HOSTNAME}_${USERNAME} $V_CVS_FILENAME
    ;;
  "sql")
    msgd "Type: sql"
    f_sql_results_to_cvs $V_ARGUMENT $HOME/local_scripto/oracle/logs/${HOSTNAME}_${USERNAME} ${V_ARGUMENT}.out
    ;;
  "sh")
    msgd "Type: sh"
    ;;
  *)
    echo "Unknown type!!! Exiting."
    exit 1
    ;;
  esac

done < $F_CONF


exit 0
f_standalone_file_to_cvs
# old stuff, do not know what to do with it


check_file $HOME/scripto/oracle/create_db_scripts/oracle_setup/oracle_setup.sh
cd $HOME/scripto/oracle/create_db_scripts/oracle_setup

if [ ! -f "$ORATAB" ]; then
  echo "No $ORATAB found. Exiting."
  exit 1
fi

# Checks done for each database present
F_ORATAB_FILTERED=/tmp/oratab_filetered_clean.tmp
cat $ORATAB | grep -v "^#"  |   grep -v '^ *$'> $F_ORATAB_FILTERED
while read ORATAB_LINE
do
  V_SID=`echo $ORATAB_LINE | awk -F":" '{print $1}'`
  msgd "V_SID: $V_SID"
  V_BIN=`echo $ORATAB_LINE | awk -F":" '{print $2}'` 
  msgd "V_BIN: $V_BIN"

  msgi "##########################################################################"
  msgi "ORACLE_SID: $V_SID"
  export ORACLE_SID=$V_SID
  msgi "################ ./oracle_setup.sh b_basic_db_healthcheck ################"
  ./oracle_setup.sh b_basic_db_healthcheck | tee -a $F_OUTPUT

  #msgi "################ ./oracle_setup.sh b_change_profile CHECK ################"
  #./oracle_setup.sh b_change_profile CHECK | tee -a $F_OUTPUT
  msgi "################ ./oracle_setup.sh b_store_in_cvs ################"
  ./oracle_setup.sh b_store_in_cvs | tee -a $F_OUTPUT
  #msgi "################ ./oracle_setup.sh b_set_audit_file_dest CHECK ################"
  #./oracle_setup.sh b_set_audit_file_dest CHECK | tee -a $F_OUTPUT

done < $F_ORATAB_FILTERED

msgi "######## Reseting the environment to easily spot #######"
msgi "######## any problems that result from DB env not set                #######"
unset ORACLE_SID
unset ORACLE_PATH
unset ORACLE_BASE
unset TNS_ADMIN
unset ORACLE_HOME
unset SQLPATH
run_command_d "env"

msgi "######## Checks done for each oracle binaries present                #######"
F_ORATAB_FILTERED=/tmp/oratab_filetered_binaries.tmp
cat $ORATAB | grep -v "^#" |   grep -v '^ *$'| awk -F":" '{print $2}' | sort -u > $F_ORATAB_FILTERED
while read ORATAB_LINE
do
  V_BIN=$ORATAB_LINE
  msgi "V_BIN: $V_BIN"

  msgi "##########################################################################"
done < $F_ORATAB_FILTERED

msgi "######## Reseting the environment to easily spot #######"
msgi "######## any problems that result from DB env not set                #######"
unset ORACLE_SID
unset ORACLE_PATH
unset ORACLE_BASE
unset TNS_ADMIN
unset ORACLE_HOME
unset SQLPATH
run_command_d "env"

#msgi "######## Checks done only once on a host                         #######"
#msgi "################ ./oracle_setup.sh b_forward_mail CHECK ################"
#./oracle_setup.sh b_forward_mail CHECK | tee -a $F_OUTPUT
#msgi "################ ./oracle_setup.sh b_check_mail_queue CHECK ################"
#./oracle_setup.sh b_check_mail_queue CHECK | tee -a $F_OUTPUT
#msgi "################ ./oracle_setup.sh b_basic_os_healthcheck ################"
#./oracle_setup.sh b_basic_os_healthcheck | tee -a $F_OUTPUT

#msgi "################ ./implement_CFC.sh CHECK ################"
#check_file $HOME/scripto/oracle/RMAN/implement_CFC.sh
#cd $HOME/scripto/oracle/RMAN
#./implement_CFC.sh CHECK | tee -a $F_OUTPUT

# WIP
#msgi "################ ./check_password.sh ################"
#if [ -f $HOME/scripto/solaris/check_password/check_password.sh ]; then
#  cd $HOME/scripto/solaris/check_password
#  ./check_password.sh | tee -a $F_OUTPUT
#fi
# OLD Stuff
#  msgi "################ Actions for binaries: $ORATAB_LINE  ################"
#  ./oracle_setup.sh b_check_and_fix_orphan_links_resulting_from_rsync_copy CHECK | tee -a $F_OUTPUT


#echo "Exiting without sending mail. "
#exit 0

if [ `cat $F_OUTPUT | wc -l` -gt 0 ]; then
  msga "Sending mail with commands output"
  cat $F_OUTPUT | tr -cd '[:graph:] [:space:]' | sed s/\\[31m//g | sed s/\\[39m\\[49m//g | $MAILCMD -s "[ promon ] $ORACLE_SID `uname -n`" "orainf@logwatch"
fi

rm -f $F_OUTPUT
